from view.com_0.content_frame_base import ContentFrameBase, init_tool_button
from boot import Union, QWidget, AvatarWidget, Qt, QLabel, QVBoxLayout, IMG_ROOT_DIR, os, FontFamily, Dict, List, QHBoxLayout, QSizePolicy
from utils.format import amount_format
from view.widget.qq9_line import QQ9Line
from view.widget.qq9_info_widget import QQ9InfoWidget
from view.widget.qq9_label import QQ9QrcodeLabel
import json
from view.widget.qq9_tabel_widget import QQ9TabelWidget


__all__ = ["SaleContentFrame"]


class SaleContentFrame(ContentFrameBase):

    def __init__(self, parent: Union[QWidget, None] = None):
        super().__init__(parent)

        self.content_frame.main_v_layout.setContentsMargins(0, 50, 0, 50)
        self.content_frame.main_v_layout.setAlignment(Qt.AlignCenter)
        self.content_frame.setFixedWidth(1200)

        # 主要信息
        self.info = InfoLabel()
        self.content_frame.main_v_layout.addWidget(self.info, alignment=Qt.AlignCenter)

        # 表
        self.table = QQ9TabelWidget()
        self.content_frame.main_v_layout.addWidget(self.table, alignment=Qt.AlignCenter)

        # 初始化无内容
        self.set_no_content()

    def set_info(self, sale_id, cust_name, sale_datetime, sale_list: str):

        sale_number = 0  # 总件数
        total_amount = 0

        # 1.将数据写入表格中
        content: List[List[str]]= json.loads(sale_list)["content"]
        self.table.table.create_read_only_table_with_content(data=content)
        # 2.计算合计金额与总件数并设置
        for row in content:
            sale_number += int(row[1]) if row[1].replace(" ", "") != "" else 0
            total_amount += int(row[5]) if row[5].replace(" ", "") != "" else 0
        self.table.total_bill.set_value(str(total_amount))
        # 3.设置主要信息
        self.info.set_text(
            sale_id=sale_id,
            cust_name=cust_name,
            sale_datetime=sale_datetime,
            sale_number=str(sale_number),
        )


class InfoLabel(QWidget):

    def __init__(self,
                    parent: Union[QWidget, None] = None,
                    sale_id: str = "",
                    cust_name: str = "",
                    sale_datetime: str = "",
                    sale_number: str = "",
                 ):
        super().__init__(parent)

        self.setFixedSize(1100, 50)

        self.main_h_layout = QHBoxLayout()
        self.main_h_layout.setContentsMargins(40, 0, 0, 0)
        self.main_h_layout.setAlignment(Qt.AlignVCenter | Qt.AlignLeft)
        self.main_h_layout.setSpacing(30)
        self.setLayout(self.main_h_layout)

        # 清单编号
        self.sale_no_info_widget = QQ9InfoWidget(title="清单编号", icon_path=IMG_ROOT_DIR + "/清单编号.png", add_style="font-weight: bold;", title_color="#909399")
        self.sale_no_info_widget.title_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.sale_no_info_widget.content_label.setContentsMargins(0, 0, 0, 0)
        self.sale_no_info_widget.content_label.setAlignment(Qt.AlignLeft)
        self.sale_no_info_widget.setFixedWidth(320)

        # 客户姓名
        self.cust_name_info_widget = QQ9InfoWidget(title="客户姓名", icon_path=IMG_ROOT_DIR + "/客户姓名.png", add_style="font-weight: bold;", title_color="#909399")
        self.cust_name_info_widget.title_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.cust_name_info_widget.content_label.setContentsMargins(10, 0, 0, 0)
        self.cust_name_info_widget.content_label.setAlignment(Qt.AlignLeft)
        self.cust_name_info_widget.setFixedWidth(200)

        # 发货时间
        self.sale_datetime_info_widget = QQ9InfoWidget(title="发货时间", icon_path=IMG_ROOT_DIR + "/发货时间.png", add_style="font-weight: bold;", title_color="#909399")
        self.sale_datetime_info_widget.title_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.sale_datetime_info_widget.content_label.setContentsMargins(10, 0, 0, 0)
        self.sale_datetime_info_widget.content_label.setAlignment(Qt.AlignLeft)
        self.sale_datetime_info_widget.setFixedWidth(250)

        # 发货件数
        self.sale_number_info_widget = QQ9InfoWidget(title="发货件数", icon_path=IMG_ROOT_DIR + "/发货件数.png", add_style="font-weight: bold;", title_color="#909399")
        self.sale_number_info_widget.title_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.sale_number_info_widget.content_label.setContentsMargins(10, 0, 0, 0)
        self.sale_number_info_widget.content_label.setAlignment(Qt.AlignLeft)
        self.sale_number_info_widget.setFixedWidth(200)

        # 放入布局
        self.main_h_layout.addWidget(self.sale_no_info_widget)
        self.main_h_layout.addWidget(self.cust_name_info_widget)
        self.main_h_layout.addWidget(self.sale_datetime_info_widget)
        self.main_h_layout.addWidget(self.sale_number_info_widget)


    def set_text(self,
                 sale_id: str = "",
                 cust_name: str = "",
                 sale_datetime: str = "",
                 sale_number: str = "",
                 ):
        self.sale_no_info_widget.set_info(sale_id)
        self.cust_name_info_widget.set_info(cust_name)
        self.sale_datetime_info_widget.set_info(sale_datetime)
        self.sale_number_info_widget.set_info(sale_number)
